package cate.game.server.schedule.task;

import cate.game.GameBody;
import cate.game.role.res.guild.po.GuildPO;
import lombok.extern.slf4j.Slf4j;

import java.util.Collection;
import java.util.List;

@Slf4j
public class EstimateHealthTask extends ProcessTaskExecutable {

    @Override
    protected String exeImpl() throws Exception {
        StringBuilder sb = new StringBuilder();
        Collection<GameBody> gameList = games.getAll();
        sb.append("游戏服数量:"+gameList.size()+";");
        int sum = 0;
        int max = 0;
        int min = 0;
        for(GameBody game : gameList) {
            if(Thread.interrupted()) {
                log.warn("线程被中断，不再继续执行了，就此打住，ByeBye");
                break;
            }
            int s = score(game);
            sum += s;
            max = Math.max(s, max);
            min = Math.min(s, min);
        }
        int num = gameList.size();
        int avg = num <= 0 ? 0 : sum/num;
        return "avg="+avg+";max="+max+";min="+min;
    }

    private static int score(GameBody game) throws Exception {
        int s = 100;
        List<GuildPO> guildList = game.res.guild.getGuildList();
        if(guildList != null) {
            s -= Math.min(25, guildList.size() / 20);
        }
        s -= Math.min(25, game.fight.task.getTaskSize()/3);
        int activeNum = game.role.getActiveNum();
        if(activeNum > 500) {
            s -= Math.min(30, (activeNum-500) / 100);
        }
        return s;
    }

}
